package com.atsmart.app.activity;

import java.util.ArrayList;
import java.util.HashSet;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.Toast;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
import com.atsmart.app.exception.GlobalExceptionHandler;
import com.atsmart.app.utils.DialogUtil;

public class BaseActivity extends Activity {
	private static Dialog dialog = null;
    protected boolean bRetryRequire_;
    private int iLatestErrorCode_;

    protected  ArrayList<Object> loadStatus;//只是管理当前activity
    protected  HashSet<Object> arrTaskFailed_; //异步任务请求失败

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        GlobalExceptionHandler.register(this);
        bRetryRequire_ = false;
        iLatestErrorCode_ = 0;
        loadStatus = new ArrayList<Object>();
    }

    @Override
    protected void onResume() {
        super.onResume();
        BizApp.getInstance().setCurrentActivity(this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		if (intent != null) {
			if (intent.getFlags() == Intent.FLAG_ACTIVITY_CLEAR_TOP) {
				// quit application now?
				if (intent.getBooleanExtra("ExitNow", true)) {
					this.finish();
					return;
				}
			}
		}
	}

    @Override
    protected void onPause() {
    	hideLoading();
        super.onPause();
    }

    protected Dialog showLoading() {
        if (dialog == null) {
            dialog = DialogUtil.getLoadingDialog(this);
            dialog.show();
        }
        return dialog;
    }
    
    protected Dialog showLoading(String message) {
        if (dialog == null) {
            dialog = DialogUtil.getLoadingDialog(this,message);
            dialog.show();
        }
        return dialog;
    }
    
    
    

    protected void hideLoading() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    protected synchronized void registerLoadStatus(Object obj) {
        if (!loadStatus.contains(obj)) {
            loadStatus.add(obj);
        }
    }

    protected synchronized boolean IsLoadingOrFailed(Object obj) {
        return arrTaskFailed_!= null && arrTaskFailed_.contains(obj);
    }

    protected synchronized void loadingFailed(Object obj, int iErrorCode) {
        if (this.arrTaskFailed_ == null){
            this.arrTaskFailed_ = new HashSet<Object>();
        }
        this.arrTaskFailed_.add(obj);//记录一项失败

        loadStatus.remove(obj);
        iLatestErrorCode_ = iErrorCode;
        if (loadStatus.size() == 0) {
            hideLoading();
            showDialog(iLatestErrorCode_);
        }
    }

    protected synchronized void finishedLoad(Object obj) {
        loadStatus.remove(obj);
        if (this.arrTaskFailed_ != null){
            arrTaskFailed_.remove(obj);
        }
        if (loadStatus.size() == 0) {
            hideLoading();
            if (this.arrTaskFailed_ != null && this.arrTaskFailed_.size() > 0) {
                showDialog(iLatestErrorCode_);
            }
        }
    }

    protected void resetRequireTask(){
        if (arrTaskFailed_ != null){
            arrTaskFailed_.clear();
        }
        bRetryRequire_ = false;
    }

    protected void retryRequire(){

    }

    @Override
    protected Dialog onCreateDialog(int id) {
      //  hideLoading();
//        Dialog dialog = null;
//        Toast.makeText(this, R.string.network_error, Toast.LENGTH_SHORT).show();
        return dialog;
    }

}
